<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-02 13:15:52
 * @Last Modified time: 2023-02-23 15:01:15
 */
namespace app\common\services\user;

use frank\base\BaseService;
use app\common\dao\user\UserAddressDao;
use frank\exception\CommonException;
use app\common\services\RegionService;

/**
 * 用户地址
 * Class UserAddressService
 */
class UserAddressService extends BaseService
{
    /**
     * UserAddressService constructor.
     * @param UserAddressDao $dao
     */
    public function __construct(UserAddressDao $dao)
    {
        $this->dao = $dao;
    }

    public function getAddressDetail($filter)
    {
        $detail = $this->dao->search($filter)->find();
        if (!$detail) {
            throw new CommonException('地址不存在');
        }
        return $detail->toArray();
    }

    public function getUserDefaultAddressDetail($uid,$addressId)
    {
        $detail = $this->dao->search(['user_id'=>$uid])
        ->where(function ($query) use($addressId) {
            $query->when($addressId,function($query) use ($addressId){
                $query->where(['id'=>$addressId]);
            })->whereOr([
                [['is_default','=',1]]
            ]);
        })->find();
        if (!$detail) {
            return [];
        }
        return $detail->toArray();
    }

    /**
     * 获取列表
     */
    public function getUserAddressList(array $where = [])
    {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getUserAddressList($where, $page, $limit);
        return $list;
    }

    /**
     * 获取全部
     */
    public function getUserAddressSelect(array $where = [])
    {
        $list = $this->dao->getUserAddressSelect($where);
        return $list;
    }
    
    public function getAddressRegionData(array $params = []){
    	$regionService = RegionService::instance();
    	$provinceId = $regionService->getIdByName($params['region'][0], 1);
        $cityId = $regionService->getIdByName($params['region'][1], 2, $provinceId);
        $regionId = $regionService->getIdByName($params['region'][2], 3, $cityId);
        // 验证城市ID是否合法
        if ($this->checkCityId($cityId)){
        	$data = [
	        	'name' => $params['name'],
	            'phone' => $params['phone'],
	            'province_id' => $provinceId,
	            'city_id' => $cityId,
	            'region_id' => $regionId,
	            'detail' => $params['detail'],
	            'is_default' => $params['is_default'],
	            'label' => $params['label'],
	            'district' => ($regionId === 0 && !empty($params['region'][2])) ? $params['region'][2] : '',
	        ];
        	return $data;
	    }
	}

    /**
     * 保存数据校验
     */
    public function checkSave($uid,$data)
    {
        //收货地址添加上限20
        $filter['user_id'] = $uid;
        $count = $this->dao->search($filter)->count();
        if ($count >= 100){
            throw new CommonException('收货地址超限(上限100)!');
        }
        if($data['is_default']){
        	$filter['is_default'] = 1;
        	$this->dao->update($filter,['is_default'=> 0]);
        }
       	if(!$count) $data['is_default'] = 1;
        $data['user_id'] = $uid;
       	return $data;
    }

    /**
     * 验证城市ID是否合法
     */
    private function checkCityId($cityId)
    {
        if ($cityId <= 0) {
            _trace('system_error','选择的城市不存在');
            throw new CommonException('很抱歉，您选择的城市不存在');
        }
        return true;
    }

}